<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="方案编号" prop="schemePlan" v-show="false">
        <el-input
          v-model="queryParams.schemePlan"
          placeholder="请输入方案编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="方案名称" prop="schemeName">
        <el-input
          v-model="queryParams.schemeName"
          placeholder="请输入方案名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡片类型" prop="cardType">
        <el-select v-model="queryParams.cardType" placeholder="请选择卡片类型">
          <el-option v-for="dict in dict.type.sys_card_type" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="起始卡号" prop="cardnoBegin">
        <el-input
          v-model="queryParams.cardnoBegin"
          placeholder="请输入起始卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="止卡号" prop="cardnoEnd">
        <el-input
          v-model="queryParams.cardnoEnd"
          placeholder="请输入止卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:emptycard:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:emptycard:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:emptycard:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:emptycard:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="emptycardList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="方案名称" align="center" prop="schemeName" />
      <el-table-column label="卡片类型" align="center" prop="cardType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_card_type" :value="scope.row.cardType"/>
        </template>
      </el-table-column>
      <el-table-column label="起始卡号" align="center" prop="cardnoBegin" />
      <el-table-column label="止卡号" align="center" prop="cardnoEnd" />
      <el-table-column label="是否储值卡" align="center" prop="isbalance">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no_num" :value="scope.row.isbalance"/>
        </template>
      </el-table-column>
      <el-table-column label="是否次卡" align="center" prop="istimes">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no_num" :value="scope.row.istimes"/>
        </template>
      </el-table-column>
      <el-table-column label="是否折扣卡" align="center" prop="isdiscount">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no_num" :value="scope.row.isdiscount"/>
        </template>
      </el-table-column>
      <el-table-column label="是否权益卡" align="center" prop="isequity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no_num" :value="scope.row.isequity"/>
        </template>
      </el-table-column>
      <el-table-column label="是否积分卡" align="center" prop="isscore">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no_num" :value="scope.row.isscore"/>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:emptycard:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:emptycard:remove']"
          >作废</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改空卡制作对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="方案类型">
              <el-select v-model="form.schemePlan" placeholder="请选择方案类型">
                <el-option v-for="item in schemeOptions" :key="item.schemePlan" :label="item.schemeName"
                           :value="item.schemePlan"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="卡片类型" prop="cardType">
              <el-select v-model="form.cardType" placeholder="请选择卡片类型">
                <el-option v-for="dict in dict.type.sys_card_type" :key="dict.value" :label="dict.label"
                           :value="Number(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="起始卡号" prop="cardnoBegin">
              <el-input v-model="form.cardnoBegin" :disabled="isEdit" placeholder="请输入起始卡号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="止卡号" prop="cardnoEnd">
              <el-input v-model="form.cardnoEnd" :disabled="isEdit" placeholder="请输入止卡号"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否次卡">
              <el-select v-model="form.istimes" placeholder="请选择是否次卡">
                <el-option v-for="dict in dict.type.sys_yes_no_num" :key="dict.value" :label="dict.label"
                           :value="Number(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否折扣卡">
              <el-select v-model="form.isdiscount" placeholder="请选择是否折扣卡">
                <el-option v-for="dict in dict.type.sys_yes_no_num" :key="dict.value" :label="dict.label"
                           :value="Number(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否权益卡">
              <el-select v-model="form.isequity" placeholder="请选择是否权益卡">
                <el-option v-for="dict in dict.type.sys_yes_no_num" :key="dict.value" :label="dict.label"
                           :value="Number(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="是否积分卡">
              <el-select v-model="form.isscore" placeholder="请选择是否积分卡">
                <el-option v-for="dict in dict.type.sys_yes_no_num" :key="dict.value" :label="dict.label"
                           :value="Number(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否储值卡">
              <el-select v-model="form.isbalance" placeholder="请选择是否储值卡">
                <el-option v-for="dict in dict.type.sys_yes_no_num" :key="dict.value" :label="dict.label"
                           :value="Number(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listEmptycard, getEmptycard, delEmptycard, addEmptycard, updateEmptycard } from "@/api/system/emptycard";
import { listScheme } from "@/api/system/scheme";

export default {
  name: "Emptycard",
  dicts: ['sys_card_type', 'sys_yes_no_num'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 空卡制作表格数据
      emptycardList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      isEdit: false,
      // 方案选项
      schemeOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        schemeId: null,
        cardnoBegin: null,
        cardnoEnd: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        /*schemePlan: [
          { required: true, message: "方案不能为空", trigger: "blur" }
        ],*/
        cardType: [
          { required: true, message: "卡片类型不能为空", trigger: "blur" }
        ],
        cardnoBegin: [
          { required: true, message: "起始卡号不能为空", trigger: "blur" }
        ],
        cardnoEnd: [
          { required: true, message: "止卡号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询空卡制作列表 */
    getList() {
      this.loading = true;
      listEmptycard(this.queryParams).then(response => {
        this.emptycardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        inputId: null,
        deptId: null,
        cardnoBegin: null,
        cardnoEnd: null,
        isbalance: 0,
        isscore: 0,
        istimes: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        lsh: null,
        isdiscount: 0,
        isequity: 0,
        schemePlan: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.inputId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加空卡制作";
      this.listScheme()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const inputId = row.inputId || this.ids
      getEmptycard(inputId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改空卡制作";
        this.isEdit = true;
      });
    },
    listScheme() {
      this.loading = true;
      listScheme(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.schemeOptions = response.rows;
        this.loading = false;
        this.isEdit = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.inputId != null) {
            updateEmptycard(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addEmptycard(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const inputIds = row.inputId || this.ids;
      this.$modal.confirm('是否确认删除空卡制作编号为"' + inputIds + '"的数据项？').then(function() {
        return delEmptycard(inputIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/emptycard/export', {
        ...this.queryParams
      }, `emptycard_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.el-select {
  display: inline-block;
  position: relative;
  width: -webkit-fill-available;
}
</style>
